package com.example.chat.config;


import com.example.chat.api.WebSocketAPI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
// 注册 所构造的websocketAPI
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private WebSocketAPI webSocketAPI;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {

        // 绑定路径信息
        registry.addHandler(webSocketAPI, "/webSocketMessage")

                // 调用拦截器，使得 WebsocketSession 从 HttpSession 中拿到数据
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
